<template>
  <div class="content_body">
    <div v-html="content"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: null
    }
  },
  mounted() {
    // console.log(this.$route.query.id);
    this.$axios.get("api/news/" + this.$route.query.id).then(({data}) => {
      // console.log(data.data.content);
      data.data.content = data.data.content.replace(/%/g, '%25');
      this.content = this.formatRichText(decodeURIComponent(data.data.content));
      // console.log(this.content);
    })
  },
  methods: {
    formatRichText(html) {
      let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
        match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
        match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
        match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
        return match;
      });
      newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
        match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
        return match;
      });
      newContent = newContent.replace(/<br[^>]*\/>/gi, '');
      newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');
      return newContent;
    }
  }
}
</script>

<style scoped>
.content_body {
  max-width: 1000px;
  margin: 0 auto;
  padding: 10px;
}
</style>
